﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Medianamik.Core;
using Medianamik.Core.Action;
using Medianamik.Core.Configuration;
using Medianamik.Core.Security;
using Medianamik.DummyWebSite.Container;
using StructureMap;

namespace Tests.Integration_Medianamik
{
    public static class TestUtils
    {
        public static void TestFixtureSetUp()
        {
            MedianamikConfigSection Config = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);
            ContainerBootstrapper.BootstrapStructureMap();

            ActionManager.RegisterActionHandler<IndexActionHandler>();
            ActionManager.RegisterActionHandler<NodeTypeGenTableActionHandler>();
            ActionManager.RegisterActionHandler<Medianamik.Core.Urls.UrlChangedActionHandler>();
            ActionManager.RegisterActionHandler<UserProfileActionHandler>();
            ActionManager.RegisterActionHandler<UpdateGenTableActionHandler>();
            ActionManager.RegisterActionHandler<UserHandler>();
            ActionManager.RegisterActionHandler<Medianamik.Core.Redirect.RedirectUrlChangedActionHandler>();
            ActionManager.RegisterActionHandler<Medianamik.Core.Logging.LoggingActionHandler>();
            ActionManager.RegisterActionHandler(String.IsNullOrEmpty(Config.UrlActionHandler) ?
                "Medianamik.UI.Controls.UrlActionHandler, Medianamik.UI" :
                Config.UrlActionHandler);
            ActionManager.RegisterActionHandler("Medianamik.UI.Lock.ReleaseLockAction, Medianamik.UI");
            ActionManager.RegisterActionHandler("Medianamik.Client.SqlCacheDependencyActionHandler, Medianamik.Client");



        }

        public static void TestFixtureTearDown()
        {
            ObjectFactory.ResetDefaults();
        }

        public static void SetUp()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-ca");

        }

        public static void TearDown()
        {

        }

        public static Node CreateAndSaveNode(Guid typeId, Guid parentId, string nodeName)
        {
            return CreateAndSaveNode(typeId, parentId, nodeName, true);
        }

        public static Node CreateAndSaveNode(Guid typeId, Guid parentId, string nodeName, bool toBePublished)
        {
            Node newNode = CreateNode(typeId, parentId, nodeName, toBePublished);
            newNode = NodeManager.SaveNode(newNode);
            return newNode;
        }

        public static Node CreateNode(Guid typeId, Guid parentId, string nodeName)
        {
            return CreateNode(typeId, parentId, nodeName, true);
        }

        public static Node CreateNode(Guid typeId, Guid parentId, string nodeName, bool toBePublished)
        {
            Node newNode = NodeManager.NewNode(typeId, CultureInfo.CurrentUICulture, parentId);
            newNode.Instances[CultureInfo.CurrentUICulture.Name.ToLower()].Properties["Name"].SetValue(nodeName);
            if (toBePublished)
                newNode.DefaultInstance.ToBePublished = true;
            return newNode;
        }
    }
}
